package com.hl.sfzy.service.content.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.content.FM;
import com.hl.sfzy.mapper.content.fm.FMMapper;
import com.hl.sfzy.service.content.FMService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class FMServiceImpl implements FMService {

    @Autowired
    private FMMapper fmMapper;

    @Override
    public PageInfo<FM> getAllList(Integer pageNum, Integer pageSize) {
        Long count = fmMapper.findCount();
        int ceil = (int) Math.ceil(count / pageSize);
        if (pageNum-1 > ceil){
            return new PageInfo<>(Collections.emptyList());
        }
        PageHelper.startPage(pageNum,pageSize);
        List<FM> byAll = fmMapper.findByAll();
        PageInfo<FM> pageInfo = new PageInfo<>(byAll);
        return pageInfo;
    }

    @Override
    public int addFM(FM fm) {
        int i = fmMapper.saveFM(fm);
        return i;
    }

    @Override
    public int removeFM(Long id) {
        return fmMapper.deleteFM(id);
    }

    @Override
    public PageInfo<FM> searchList(String keyword, LocalDateTime start, LocalDateTime endTime, Integer pageNum, Integer pageSize) {
        System.out.println(keyword);
        PageHelper.startPage(pageNum,pageSize);
        List<FM> byAll = fmMapper.search(keyword,start,endTime);
        PageInfo<FM> pageInfo = new PageInfo<>(byAll);
        return pageInfo;
    }

    @Override
    public FM getFMById(Long id) {
        return fmMapper.findById(id);
    }

    @Override
    public int updateFM(FM fm) {
        return fmMapper.updateFM(fm);
    }


}
